package com.wanmait.wisdom.manage.controller;

import com.wanmait.wisdom.util.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("manage")
public class ManagePicController {
    //上传照片
    @PostMapping("/uploadPic")
    public AjaxResult uploadPic(MultipartFile file){
        try {
            String path = ResourceUtils.getURL("classpath:").getPath() +"/static/uploadPic";
            String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ FilenameUtils.getExtension(file.getOriginalFilename());
            file.transferTo(new File(path,fileName));
            return AjaxResult.success("上传成功").put("fileName",fileName);
        } catch (IOException e) {
            return AjaxResult.error("上传失败");
        }
    }
    //上传身份证正面
    @PostMapping("/uploadCardPicFacade")
    public AjaxResult uploadCardPicFacade(MultipartFile file){
        try {
            String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadCardPicFacade";
            String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+FilenameUtils.getExtension(file.getOriginalFilename());
            file.transferTo(new File(path,fileName));
            return AjaxResult.success("上传成功").put("fileName",fileName);
        } catch (IOException e) {
            return AjaxResult.error("上传失败");
        }
    }
    //上传身份证反面
    @PostMapping("/uploadCardPicBack")
    public AjaxResult uploadCardPicBack(MultipartFile file){
        try {
            String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadCardPicBack";
            String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ FilenameUtils.getExtension(file.getOriginalFilename());
            file.transferTo(new File(path,fileName));
            return AjaxResult.success("上传成功").put("fileName",fileName);
        } catch (IOException e) {
            return AjaxResult.error("上传失败");
        }
    }
}
